package com.itheima.ThreadCommunication;

//了解线程通信流程
// 使用三个爸爸存钱，两个孩子取钱模拟； 必须一存一取  存10块，取10块
public class ThreadDemo {
    public static void main(String[] args) {
        Account account = new Account(0);
        // 2. 创建2个线程取钱
        Thread thread1 = new DrawThread("小明", account);
        Thread thread2 = new DrawThread("小红", account);
        thread1.start();
        thread2.start();
        // 3. 创建3个存钱线程
        Thread thread3 = new DepositThread("父亲1号", account);
        Thread thread4 = new DepositThread("父亲2号", account);
        Thread thread5 = new DepositThread("父亲3号", account);
        thread3.start();
        thread4.start();
        thread5.start();

    }
}

class Account {
    private double money;

    public Account(double money) {
        this.money = money;
    }

    public Account() {
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
    //取钱
    public synchronized void withDraw(double money) {
        try {
            String name = Thread.currentThread().getName();
            if (getMoney()>=money){
                System.out.println(name+"取钱:"+money);
                this.money -= money;
                System.out.println("余额:"+this.money);
                //取完就没有钱了
                this.notifyAll();
                this.wait();
            }else {
                // 如果我第一次来，没有钱，怎么办，唤醒存钱的，自己等待
                // 怎么唤醒呢
                System.out.println(name+"余额不足！");
                this.notifyAll();  //唤醒所有线程；先唤醒再睡觉
                this.wait();

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void deposit(double money) {
        String name = Thread.currentThread().getName();
        try {
            if (this.money == 0) {
                //没钱了，存钱
                this.money += money;
                System.out.println(name+"存钱");
                System.out.println("余额:"+this.money);
                //唤醒别人，等待自己
                this.notifyAll();
                this.wait();
            }else{
                //有钱了，不存
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class DrawThread extends Thread {
    private Account acc;

    public DrawThread(String name, Account acc) {
        super(name);
        this.acc = acc;
    }

    @Override
    public void run() {
        // 不断取钱
        while (true) {
            acc.withDraw(1000);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class DepositThread extends Thread {
    private Account acc;

    public DepositThread(String name, Account acc) {
        super(name);
        this.acc = acc;
    }

    @Override
    public void run() {
        // 不断存钱
        while (true) {
            acc.deposit(1000);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}